import { getToken } from '@/utils/token'
import { ElMessage } from 'element-plus'
import { router } from '@/router'
import NProgress from 'nprogress'
import store from './store'

const allowedAccess = ['/login']
router.beforeEach(async(to, from, next) => {
  console.log(to)
  NProgress.start()
  const hasToken = getToken()
  if (allowedAccess.includes(to.path)) {
    if (hasToken && to.path === '/login') {
      ElMessage.warning('当前已处于登录状态,如希望前往登录页,请通过退出登录操作')
      next('/')
      NProgress.done()
    } else {
      next()
      NProgress.done()
    }
  } else {
    if (hasToken) {
      next()
      // 获取当前面包屑
      await store.dispatch('breadcrumb/getBreadList', to)
    } else {
      next('/login')
    }
    NProgress.done()
  }
})
